<template>
  <div class="tagsOuter" :style="{ background: `url(${bgImg}) no-repeat`, backgroundSize: 'cover' }">
    <HeaderPage :headerNavindex="5" />
    <FooterPage :fixed="true" />

    <div class="tagsMainOuter" v-if="tagData.length <1?false:true">
      <div class="tagsMain" v-for="item in tagData" :key="item._id" @click="getTagItem(item.tagName)">
        {{item.tagName}}<span></span>
      </div>
    </div>

    <div class="tagsMainOuter" v-if="tagData.length <1">
      <h3>数据为空</h3>
    </div>

    <div class="articleList" v-if="tagList.length !==0">
      <div class="articleListItem" v-for="item in tagList" :key="item._id" @click="toArticleDetile(item._id)">
        <img :src="item.articleCover" alt="背景">
        <p>{{item.title}}</p>
        <p>{{item.introduction}}</p>
        <div class="bottom">
          <span>{{item.category}}</span>
          <span>{{item.tag}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

import HeaderPage from '../../../components/HeaderPage.vue';
import FooterPage from '../../../components/FooterPage.vue';
import { ElMessage } from 'element-plus';

import { getTagApi } from '../../../service/apiModule/tagApi'
import { findImgApi } from '../../../service/apiModule/bgImgApi'
import { searchArticleOneApi } from '../../../service/apiModule/articleApi'

const router = useRouter()

onMounted(() => {
  getTag()
  getBgImg()
})

const bgImg = ref('')

// 获取背景
const getBgImg = async () => {
  const { data: res } = await findImgApi()
  if (res.status.code !== 200) {
    bgImg.value = 'http://nevergiveupt.top/tags.jpg'
  } else {
    res.data.forEach((item: any) => {
      if (item.bgTitle === '标签背景') {
        bgImg.value = item.imgSrc
      }
    })
    console.log(res.data)
  }
}

const tagData = ref([{
  articleNum: 0,
  tagName: "",
  tagStatus: false,
  _id: ''
}])

// 获取标签
const getTag = async () => {
  const { data: res } = await getTagApi({ name: "" })
  if (res.status.code !== 200) {
    ElMessage.error('标签获取失败!')
    tagData.value = []
  } else {
    const arr: any = []
    res.data.list.forEach((item: any) => {
      if (item.tagStatus) {
        arr.push(item)
      }
    })
    tagData.value = arr
    console.log(tagData.value)
  }
}

const tagList = ref([])

// 获取一种分类
const getTagItem = async (data: String) => {
  const { data: res } = await searchArticleOneApi({
    type: 'tag',
    title: data
  })
  tagList.value = res.data
  console.log('标签', tagList.value)
}

// 去文章详情
const toArticleDetile = (id: any) => {
  router.push({
    path: '/article/detail',
    query: { id }
  })
}

</script>

<style lang="scss" scoped>
.tagsOuter {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  color: white;
  position: relative;
  top: 0;
  padding-top: 80px;

  .tagsMainOuter {
    width: 80%;
    height: 200px;
    margin: 0 auto;
    margin-top: 100px;
    background-color: rgba(206, 255, 253, 0.9);
    color: black;
    padding: 20px;
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding-left: 50px;
    border-radius: 10px;
    box-shadow: 0 0 10px 10px rgba(0, 204, 255, 0.3);

    .tagsMain {
      height: 34px;
      padding: 0 20px;
      text-align: center;
      background-color: rgba(133, 133, 255, .6);
      box-shadow: 0 0 10px 2px rgb(195, 195, 195);
      border-radius: 10px;
      font-size: 17px;
      font-family: '楷体';
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 30px;
      cursor: pointer;

      span {
        display: inline-block;
        font-size: 13px;
        color: rgb(231, 1, 1);
        font-weight: bold;
        margin-left: 6px;
      }
    }
  }

  .articleList {
    width: 80%;
    min-height: 120px;
    border: 1px solid;
    margin: 0 auto;
    margin-top: 20px;
    background-color: #d8d8d8;
  }

  .articleList {
    width: 80%;
    min-height: 160px;
    border: 1px solid;
    margin: 0 auto;
    margin-top: 20px;
    background-color: rgba(215, 251, 255, 0.9);
    padding: 10px;
    border-radius: 10px;

    .articleListItem {
      background-color: #fff;
      height: 160px;
      width: 140px;
      color: #000;
      cursor: pointer;

      img {
        width: 100%;
        height: 80px;
      }

      &>p {
        font-size: 14px;

        &:nth-child(2) {
          font-weight: bold;
          text-align: center;
          margin: 6px 0;
        }

        &:nth-child(3) {
          margin: 6px 0;
          font-size: 13px;
          padding-left: 10px;
        }
      }

      .bottom {
        font-size: 12px;
        width: 50%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 10px;
      }
    }
  }
}
</style>